class_name RangedWeaponBase
extends WeaponBase

# 远程武器基础属性
var bullet_speed: float    = 800.0
var bullet_lifetime: float = 3.0
var penetration: bool      = false
var bullets_per_shot: int  = 1

# 组件引用
@onready var shoot_position: Marker2D = $ShootPosition
@onready var animated_sprite: AnimatedSprite2D = $GunAnimatedSprite2D

# 子弹场景路径
var bullet_scene_path: String = "res://scene/weapon/bullet/bullet.tscn"


func get_animated_sprite() -> AnimatedSprite2D:
	return animated_sprite


func _ready() -> void:
	super._ready()

	# 连接动画完成信号
	if get_animated_sprite() and not get_animated_sprite().animation_finished.is_connected(_on_animation_finished):
		get_animated_sprite().animation_finished.connect(_on_animation_finished)


func load_weapon_data() -> void:
	super.load_weapon_data()

	var data = WeaponDataManager.get_weapon_data(weapon_id)
	if data.is_empty():
		return

	bullet_speed = data.get("bullet_speed", 800.0)
	bullet_lifetime = data.get("bullet_lifetime", 3.0)
	penetration = data.get("penetration", false)
	bullets_per_shot = data.get("bullets_per_shot", 1)

	if data.has("bullet_scene"):
		bullet_scene_path = data["bullet_scene"]


# 攻击实现
func attack() -> void:
	if not can_attack:
		return

	super.attack()

	# 播放攻击动画
	var anim_name: String = weapon_id + "_attack"
	if get_animated_sprite().sprite_frames.has_animation(anim_name):
		get_animated_sprite().play(anim_name)
	else:
		# 如果没有特定武器的动画，播放默认动画
		get_animated_sprite().play("attack")

	# 发射子弹
	fire_projectiles()


# 发射子弹 - 基类方法，可被子类重写
func fire_projectiles() -> void:
	var bullet_scene = load(bullet_scene_path)
	if not bullet_scene:
		printerr("无法加载子弹场景: ", bullet_scene_path)
		_finish_attack()
		return

	# 获取基础方向
	var base_direction: Vector2 = get_attack_direction()

	# 发射单个子弹（基本行为）
	spawn_bullet(bullet_scene, base_direction, 0)

	# 如果没有动画，手动完成攻击
	if not animated_sprite.sprite_frames.has_animation(weapon_id + "_attack"):
		_finish_attack()


# 生成单个子弹的辅助方法
func spawn_bullet(bullet_scene, base_direction: Vector2, angle_offset: float = 0) -> void:
	var bullet_instance = bullet_scene.instantiate()
	bullet_instance.damage = attack_power
	bullet_instance.speed = bullet_speed
	bullet_instance.lifetime = bullet_lifetime
	bullet_instance.penetration = penetration

	# 设置子弹位置
	bullet_instance.position = shoot_position.global_position

	# 应用角度偏移
	var rot: float = base_direction.angle() + deg_to_rad(angle_offset)
	bullet_instance.direction = Vector2(cos(rot), sin(rot))

	# 添加到场景
	get_tree().root.add_child(bullet_instance)


# 获取攻击方向
func get_attack_direction() -> Vector2:
	var base_direction

	if is_facing_left():
		base_direction = Vector2(-cos(global_rotation), -sin(global_rotation))
	else:
		base_direction = Vector2(cos(global_rotation), sin(global_rotation))

	return base_direction.normalized()


# 当攻击动画完成时
func _on_animation_finished() -> void:
	if get_animated_sprite().animation.ends_with("_attack"):
		_finish_attack()
		# 切换回默认动画
		var default_anim: String = weapon_id + "_default"
		if get_animated_sprite().sprite_frames.has_animation(default_anim):
			get_animated_sprite().play(default_anim)
		else:
			get_animated_sprite().play("default")


# 处理翻转
func handle_flip(flip_h: bool) -> void:
	if get_animated_sprite():
		get_animated_sprite().flip_h = flip_h

	# 调整射击位置
	if shoot_position:
		var pos_x = abs(shoot_position.position.x)
		shoot_position.position.x = -pos_x if flip_h else pos_x
